﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace spriteeditor.Control
{
    public partial class TimeLine : UserControl
    {
        int _colCount;
        public int ColCount
        {
            get
            {
                return _colCount;
            }
            set
            {
                _colCount = value;
                UpdateUI();
            }
        }
        int _rowCount = 1;
        public int RowCount
        {
            get
            {
                return _rowCount;
            }
            set
            {
                _rowCount = value;
                UpdateUI();
            }
        }
        public TimeLine()
        {
            InitializeComponent();
            rootPanel = splitContainer1.Panel2;// new MyPanel();
            //rootPanel.AutoScroll= true;
            //splitContainer1.Panel2.Controls.Add(rootPanel);
            //rootPanel.Dock = DockStyle.Fill;

            lablePanel = panel1;// new Panel();
                                // splitContainer1.Panel1.Controls.Add(lablePanel);
            lablePanel.AutoSize = true;

            rootPanel.AutoScroll = true;

            rootPanel.Scroll += (s, e) =>
             {
                 //对齐两个面板的简单方法
                 lablePanel.Top = rootPanel.AutoScrollPosition.Y;

                 //遮盖用
                 hScrollBar1.Visible = rootPanel.HorizontalScroll.Visible;
             };
            rootPanel.MouseWheel += (s, e) =>
            {
                //对齐两个面板的简单方法
                lablePanel.Top = rootPanel.AutoScrollPosition.Y;

                //遮盖用
                hScrollBar1.Visible = rootPanel.HorizontalScroll.Visible;
            };

        }


        Panel rootPanel;
        Panel lablePanel;
        PictureBox focus;
        void UpdateUI()
        {
            rootPanel.Controls.Clear();
            lablePanel.Controls.Clear();

            for (var j = 0; j < _rowCount; j++)
            {
                var label = new Label();
                label.Left = 0;
                label.Top = j * 24;
                label.Height = 24;
                label.Text = "Row:" + j;
                lablePanel.Controls.Add(label);
                for (var i = 0; i < _colCount; i++)
                {
                    var pic = new System.Windows.Forms.PictureBox();
                    pic.Image = imageList1.Images[0];
                    pic.SizeMode = PictureBoxSizeMode.Zoom;
                    pic.Left = i * 12;
                    pic.Top = j * 24;
                    pic.Width = 12;
                    pic.Height = 24;
                    pic.BorderStyle = BorderStyle.FixedSingle;
                    pic.MouseDown += (s, e) =>
                    {
                        if (focus != null)
                            focus.BackColor = Color.White;
                        focus = pic;
                        focus.BackColor = Color.Red;
                    };
                    rootPanel.Controls.Add(pic);
                }
            }
        }
    }
}
